#!/bin/bash

. /opt/rancher/common.sh
wait_for_all_service_containers

cd $(dirname $0)

GALERA_CONF='/etc/mysql/conf.d/001-galera.cnf'

echo "Waiting for Config..."
while [ ! -f "${GALERA_CONF}" ] && [ ! -f "/opt/rancher/cluster_ips" ]; do
   sleep 1
done
echo "Starting galera..."

if [ "$#" -eq "0" ]; then
    bootstrap="false"

    /opt/rancher/lowest_idx.sh
    if [ "$?" -eq "0" ] && [ ! -f '/opt/rancher/initialized' ]; then
        bootstrap="true"
    fi

    connect_string="--wsrep_cluster_address=gcomm://$(tr '\n' ',' <cluster_ips|sed 's/,.$//')"
    if [ "${bootstrap}" = "true" ]; then
        connect_string="--wsrep_cluster_address=gcomm://"
        touch /opt/rancher/initialized
    fi

    ## Incase this is the initial startup.
    if [ "${bootstrap}" = "false" ]; then
        random_sleep
    fi

    set -- mysqld "${connect_string}"
fi 

exec /docker-entrypoint.sh "$@"
